package com.drojian.demo1.myself;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.health.connect.datatypes.StepsCadenceRecord;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyView4 extends View {
    public MyView4(Context context) {
        super(context);
    }

    public MyView4(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView4(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        Paint paintText = new Paint();
        paintText.setTextSize(32);

        //线
        canvas.drawLine((float) 50,(float) 300,(float) 500,(float) 100,new Paint());
        //圆
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//空心
        paint.setStrokeWidth((float)5);     //线宽
        canvas.drawCircle(80,80,30,paint);
        canvas.drawText("圆",65,160,paintText);
        //弧形
        RectF rectF = new RectF(200,200,900,600);//距离view的距离，相减为矩形的大小
        Paint paint1 = new Paint();
        paint1.setStyle(Paint.Style.STROKE);//空心
        paint1.setStrokeWidth((float)5);     //线宽
        canvas.drawArc(rectF,(float) 0,(float)30 ,true, paint1);
        canvas.drawText("弧形",750,550,paintText);

        //内切矩形
        Paint paint2 = new Paint();
        paint2.setColor(Color.BLUE); // 设置椭圆的颜色为蓝色
        paint2.setStyle(Paint.Style.STROKE); // 设置填充模式，如果要绘制空心椭圆，可以改为Paint.Style.STROKE
            // 定义椭圆所在的矩形区域左上角(50, 400)，右下角(300, 550)，这决定了椭圆的大小和位置
        RectF ovalRect = new RectF(50, 400, 300, 550);
        canvas.drawOval(ovalRect, paint2);// 使用Canvas的drawOval()方法绘制椭圆
        canvas.drawText("矩形的内切(椭)圆",55,500,paintText);

        // 创建画笔
        Paint paint3 = new Paint();
        paint3.setColor(Color.RED); // 设置路径的颜色为红色
        paint3.setStyle(Paint.Style.STROKE); // 设置描边模式，这样路径线条可见
        paint3.setStrokeWidth(5); // 设置线条宽度
        //画路径

        Path path = new Path();// 创建路径
        path.moveTo(50, 700);//移动到起始点，如果不调用moveTo，默认从(0,0)开始
        path.lineTo(200, 900);
        path.lineTo(300, 800);
        path.lineTo(400, 1200);
        path.lineTo(500, 800);
        path.close();
        // 使用Canvas的drawPath()方法绘制路径
        canvas.drawPath(path, paint3);
        canvas.drawText("封闭折线",200,1000,paintText);

        super.onDraw(canvas);



    }
}
